package com.wbpay.sdk.model;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.wbpay.sdk.utils.Utils;

/**
 * sdk相关参数
 */
public class SdkInfo extends DeviceInfo {
    private Context mContext;
    private static SdkInfo sSdkInfo;
    private long loginExpireTime = 24 * 3600 * 1000 * 360;

    public void init(String appId, String channel) {
        setDeviceInfo(appId, channel);
    }

    private SdkInfo(Context context) {
        mContext = context.getApplicationContext();
    }

    public static SdkInfo get(Context context) {
        if (sSdkInfo == null) {
            sSdkInfo = new SdkInfo(context);
        }
        return sSdkInfo;
    }

    private void setDeviceInfo(String appId, String channel) {
        setAppId(appId);
        setChannel(channel);
        if (mContext.checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
            String simOperatorName = tm.getSubscriberId();
            String phoneNumber = tm.getLine1Number();
            String imei = tm.getDeviceId();
            setImei(imei);
            setIccid(tm.getSimSerialNumber());
            setImsi(simOperatorName);

            if (simOperatorName != null) {
                String ProvidersName = "";
                if (simOperatorName.startsWith("46000") || simOperatorName.startsWith("46002")) {
                    ProvidersName = "中国移动";
                } else if (simOperatorName.startsWith("46001")) {
                    ProvidersName = "中国联通";
                } else if (simOperatorName.startsWith("46003")) {
                    ProvidersName = "中国电信";
                }
                setProvidersName(ProvidersName);
            }
            if (!TextUtils.isEmpty(phoneNumber)) {
                if (phoneNumber.contains("+86")) {
                    phoneNumber = phoneNumber.substring(3);
                }
            }
            setPhoneNum(phoneNumber);
        }
        setModel(android.os.Build.MODEL);
        String os = android.os.Build.VERSION.RELEASE;
        setOs(os);
        setCpu(Utils.getCpuName());
        setHz(Utils.getMaxCpuFreq());
        setRam(Utils.formatSize(Utils.getTotalMemory()));
        setRom(Utils.formatSize(Utils.getTotalInternalMemorySize()));
        setNetType(Utils.getNetworkType(mContext));
        setMade(android.os.Build.MANUFACTURER);
        /*
        if (AndroidUtil.getRootAhth()) {
            setRoot("1");
        } else {
            setRoot("0");
        }*/
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();

        int width = dm.widthPixels;
        int height = dm.heightPixels;
        setDisplay(width + "," + height);
    }

    public long getLoginExpireTime() {
        return loginExpireTime;
    }
}
